<?php
namespace App\Plugins\Sms;

/**
 * Created by PhpStorm.
 * User: cc
 * Date: 2016/10/27
 * Time: 15:43
 */
class SendInfo
{
    /*
     * 获取URL数据
     */
    public function curl($url, $data = null)
    {
        //初始化
        $curl = curl_init();
        //设置选项，包括URL
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)) {
            //POST数据
            curl_setopt($curl, CURLOPT_POST, 1);
            //POST变量
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        //执行并获取HTML文档内容
        $output = curl_exec($curl);
        //释放curl句柄
        curl_close($curl);
        return $output;

    }
    /*
     *  发送短信接口
     */
    //action=send&userid=12&account=账号&password=密码&mobile=15023239810,13527576163&content=内容&sendTime=&extno=
    public function Send($code, $_filter = [], $mobile = "", $sendTime = null, $extno = null)
    {
        if (empty($mobile)) {
            return $this->json(1, 'error', '手机号码不能为空');
        }
        if (empty($code)) {
            return $this->json(5, 'error', 'code不能为空');
        }
        $config = include app_path('/Plugins/Sms/SendInfoConfig.php');
        $content = '';
        if ($code == 1) {//创建渠道发送短信
            if (empty($_filter['password'])) {
                return $this->json(2, 'error', '密码不能为空');
            }
            if (empty($_filter['alias'])) {
                return $this->json(3, 'error', '别名不能为空');
            }
            $content = str_replace('&a', $_filter['password'], $config['creatCompany']);
            $content = str_replace('&b', $_filter['alias'], $content);
        } else if ($code == 2) { // 重置密码
            if (empty($_filter['password'])) {
                return $this->json(2, 'error', '密码不能为空');
            }
            if (empty($_filter['alias'])) {
                return $this->json(3, 'error', '别名不能为空');
            }
            $content = str_replace('&pwd', $_filter['password'], $config['resetPassword']);
            $content = str_replace('&alias', $_filter['alias'], $content);
        }
        $data = [
            'mobile' => $mobile,
            'content' => $content,
            'userid' => $config['id'],
            'sendTime' => $sendTime,
            'extno' => $extno,
            'account' => $config['username'],
            'password' => $config['password'],
        ];
        try {
            $xmlData = $this->curl($config['sendUrl'], $data);
            //将XML数据转化为JSON数据
            return $this->xmlToJson($xmlData, 'send.txt', 'Success');
        } catch (\Exception $e) {
            file_put_contents(storage_path("logs/senderror.txt"), $e->getMessage() . PHP_EOL, FILE_APPEND);
        }
    }


    /*
    * 输出json格式数据
    */
    public function json($code = 0, $msg = '', $arr = [])
    {
        $data = [
            'code' => $code,
            'msg' => $msg,
            'data' => $arr
        ];
        return json_encode($data);
    }

    /*
     * 将XML转化为JSON格式数据
     */
    public function xmlToJson($xmlData, $logName, $message)
    {
        libxml_disable_entity_loader(true);
        $xmlstring = simplexml_load_string($xmlData, 'SimpleXMLElement', LIBXML_NOCDATA);
        $val = json_encode($xmlstring);
        $val_arr = json_decode($val, true);
        if ($val_arr['returnstatus'] != $message) {
            $val_arr = $this->arrToStr($val_arr);
            file_put_contents(storage_path("logs/{$logName}"), $val_arr . PHP_EOL, FILE_APPEND);
        };
        return $val;
    }

    /*
     * 重组数据
     */
    public function arrToStr($arr = [])
    {
        if (is_array($arr)) {
            foreach ($arr as $key => $value) {
                if (is_array($value)) {
                    if (empty($value)) {
                        unset($arr[$key]);
                    } elseif (!empty($value)) {
                        $arr[$key] = implode(',', $value);
                    }
                    foreach ($arr as $k => $v) {
                        $arr[$k] = $k . ':' . $v;
                        $str = implode(' ', $arr);
                    }
                    return $str;
                }
            }
            foreach ($arr as $one => $two) {
                $arr[$one] = $one . ':' . $two;
                $str = implode(' ', $arr);
            }
            return $str;
        }
    }
}